const multer = require("multer");
const path = require("path"); //
const resolve = (dir) => {
  return path.join(__dirname, "./", dir);
};
let temp = multer.diskStorage({

  destination: function (req, file, cb) {
    if (file.mimetype.match('image.*') || file.mimetype.match('video.*')) {
      cb(null, resolve("../../public/images"));
    } else {
      cb({ error: "Mime type not supported" });
    }
  },

  filename: function (req, file, cb) {
    let fileFormat = file.originalname.split(".");
    const min = 1000;
    const max = 9999;
    const randomNum = Math.floor(Math.random() * (max - min + 1)) + min;
    cb(null, new Date().getTime() + "" + randomNum + "." + fileFormat[fileFormat.length - 1]);
  },
});

const multerConfig = multer({
  storage: temp,
});
module.exports = multerConfig;
